﻿// GigeCamSettingDlg.cpp: 实现文件
//

#include "pch.h"
#include "GigeCamSettingDlg.h"
#include "afxdialogex.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CGigeCamSettingDlg 对话框

IMPLEMENT_DYNAMIC(CGigeCamSettingDlg, CDialogEx)

CGigeCamSettingDlg::CGigeCamSettingDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_GIGECAM, pParent)
	, m_fExposureTime(0)
	, m_fGain(0)
{

}

CGigeCamSettingDlg::~CGigeCamSettingDlg()
{
}

void CGigeCamSettingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_CAMNAME, m_comboCamName);
	DDX_Control(pDX, IDC_RADIO_CONTINUE_MODE, m_radioContinueMode);
	DDX_Control(pDX, IDC_RADIO_TRIGGER_MODE, m_radioTriggerMode);
	DDX_Text(pDX, IDC_EDIT_EXPOSURE, m_fExposureTime);
	DDX_Text(pDX, IDC_EDIT_GAIN, m_fGain);
}


BEGIN_MESSAGE_MAP(CGigeCamSettingDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_OPEN_DEVICE, &CGigeCamSettingDlg::OnBnClickedButtonOpenDevice)
	ON_BN_CLICKED(IDC_BUTTON_INIT, &CGigeCamSettingDlg::OnBnClickedButtonInit)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE_DEVICE, &CGigeCamSettingDlg::OnBnClickedButtonCloseDevice)
	ON_BN_CLICKED(IDC_RADIO_CONTINUE_MODE, &CGigeCamSettingDlg::OnBnClickedRadioContinueMode)
	ON_BN_CLICKED(IDC_RADIO_TRIGGER_MODE, &CGigeCamSettingDlg::OnBnClickedRadioTriggerMode)
	ON_BN_CLICKED(IDC_BUTTON_START_GRAB, &CGigeCamSettingDlg::OnBnClickedButtonStartGrab)
	ON_BN_CLICKED(IDC_BUTTON_STOP_GRAB, &CGigeCamSettingDlg::OnBnClickedButtonStopGrab)
	ON_BN_CLICKED(IDC_SOFTWARE_ONCE_BUTTON, &CGigeCamSettingDlg::OnBnClickedSoftwareOnceButton)
	ON_BN_CLICKED(IDC_BUTTON_SAVE_BMP, &CGigeCamSettingDlg::OnBnClickedButtonSaveBmp)
	ON_BN_CLICKED(IDC_BUTTON_SET_PARA, &CGigeCamSettingDlg::OnBnClickedButtonSetPara)
END_MESSAGE_MAP()


// CGigeCamSettingDlg 消息处理程序




BOOL CGigeCamSettingDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	//if (m_cGigeCam != nullptr)
	//{
	//	delete m_cGigeCam;
	//	m_cGigeCam = nullptr;
	//}
	//else
	//{
	//	m_cGigeCam = new CGigECam;
	//}
	ReadConfigToEditValue();
	// TODO:  在此添加额外的初始化
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CGigeCamSettingDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类
	//delete m_cGigeCam;
	CDialogEx::OnCancel();
}

void CGigeCamSettingDlg::ReadConfigToEditValue()
{
	m_fExposureTime = gSystemMgr.m_sCameraInf.m_fExposureTime ;
	m_fGain = gSystemMgr.m_sCameraInf.m_fGain ;

	if (gSystemMgr.m_sCameraInf.m_nTrggleModel)
	{
		m_radioTriggerMode.SetCheck(TRUE);
		m_radioContinueMode.SetCheck(FALSE);
	}
	else
	{
		m_radioTriggerMode.SetCheck(FALSE);
		m_radioContinueMode.SetCheck(TRUE);
	}
	UpdateData(false);
}

void CGigeCamSettingDlg::ReadEditValueToConfig()
{
	CString cstr;
	UpdateData(TRUE);
	const auto state = dynamic_cast<CButton *>(GetDlgItem(IDC_RADIO_CONTINUE_MODE))->GetCheck();
	state == 0 ? gSystemMgr.m_sCameraInf.m_nTrggleModel = MV_TRIGGER_MODE_ON : gSystemMgr.m_sCameraInf.m_nTrggleModel = MV_TRIGGER_MODE_OFF;

	m_comboCamName.GetWindowTextW(cstr);
	gSystemMgr.m_sCameraInf.m_cCameraInfo = cstr;
	gSystemMgr.m_cCameraSetting.SaveConfigIni(L"CameraConfig", L"m_cCameraInfo", cstr);

	gSystemMgr.m_sCameraInf.m_fExposureTime = m_fExposureTime;
	gSystemMgr.m_sCameraInf.m_fGain = m_fGain;

	cstr.Format(L"%f", gSystemMgr.m_sCameraInf.m_fExposureTime);
	gSystemMgr.m_cCameraSetting.SaveConfigIni(L"CameraConfig", L"m_fExposureTime", cstr);

	cstr.Format(L"%f", gSystemMgr.m_sCameraInf.m_fGain);
	gSystemMgr.m_cCameraSetting.SaveConfigIni(L"CameraConfig", L"m_fGain", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sCameraInf.m_nTrggleModel);
	gSystemMgr.m_cCameraSetting.SaveConfigIni(L"CameraConfig", L"m_nTrggleModel", cstr);
}

void CGigeCamSettingDlg::OnBnClickedButtonInit()
{
	m_comboCamName.ResetContent();
	m_cGigeCam.GigECamInit();
	for (unsigned int i = 0; i < m_cGigeCam.m_stDevList.nDeviceNum; i++)
	{
		m_comboCamName.AddString(m_cGigeCam.m_cstrGigeCamEnumName[i]);
		m_comboCamName.SetCurSel(0);
	}
	CamDisplay.MemProc = &CGigeCamSettingDlg::CamDisplayThread;
	UINT nThreadId;
	HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, CamDisplay.ThreadProc, (LPVOID)this, 0, &nThreadId);
	if (hThread == NULL)
	{
		gSystemLog.DisplayLogAndSave(L"Create CamDisplay thread fail", LV_ERROR);
	}
}

unsigned int __stdcall CGigeCamSettingDlg::CamDisplayThread()
{
	while (1)
	{
		if (!m_cGigeCam.m_bStartGrabbing)
		{
			Sleep(10);
			continue;
		}
		Sleep(10);
		gSystemMgr.ShowImage(m_cGigeCam.m_matBmpbuf, this,IDC_SHOW_IMAGE);
	}
}



void CGigeCamSettingDlg::OnBnClickedButtonOpenDevice()
{
	int nErrCode;
	m_cGigeCam.OpenDevice(m_comboCamName.GetCurSel());
	nErrCode = m_cGigeCam.SetTriggerMode();
	if (MV_OK != nErrCode)
	{
		gSystemLog.DisplayLogAndSave(L"相机设备打开失败！", LV_ERROR);
		return;
	}
	gSystemLog.DisplayLogAndSave(L"相机设备打开成功！",LV_EVENT);
}

void CGigeCamSettingDlg::OnBnClickedButtonCloseDevice()
{
	int nErrCode;
	if (m_cGigeCam.m_bStartGrabbing)
	{
		nErrCode = m_cGigeCam.StopGrabbing();//停止采集
		if (MV_OK != nErrCode)
		{
			gSystemLog.DisplayLogAndSave(L"Stop Grabbing Fail!", LV_ERROR);
		}
	}
	
	nErrCode = m_cGigeCam.CloseDevice();//关闭设备
	if (MV_OK != nErrCode)
	{
		gSystemLog.DisplayLogAndSave(L"Close Device Fail!", LV_ERROR);
	}
	return;
}




void CGigeCamSettingDlg::OnBnClickedRadioContinueMode()
{
	int nErrCode;
	UpdateData(TRUE);
	m_cGigeCam.m_nTriggerMode = MV_TRIGGER_MODE_OFF;
	if (!m_cGigeCam.m_bOpenDevice)
	{
		return;
	}
	
	nErrCode = m_cGigeCam.SetTriggerMode();
	if (MV_OK != nErrCode)
	{
		return;
	}
}


void CGigeCamSettingDlg::OnBnClickedRadioTriggerMode()
{
	int nErrCode;
	UpdateData(TRUE);
	m_cGigeCam.m_nTriggerMode = MV_TRIGGER_MODE_ON;
	if (!m_cGigeCam.m_bOpenDevice)
	{
		return;
	}

	nErrCode = m_cGigeCam.SetTriggerMode();
	if (MV_OK != nErrCode)
	{
		return;
	}
}


void CGigeCamSettingDlg::OnBnClickedButtonStartGrab()
{
	int nErrCode;
	UpdateData(TRUE);
	nErrCode = m_cGigeCam.GigECamRun(m_comboCamName.GetCurSel());
}


void CGigeCamSettingDlg::OnBnClickedButtonStopGrab()
{
	int nErrCode;
	nErrCode = m_cGigeCam.StopGrabbing();//停止采集
	if (MV_OK != nErrCode)
	{
		gSystemLog.DisplayLogAndSave(L"Stop Grabbing Fail!", LV_ERROR);
		return;
	}
	gSystemLog.DisplayLogAndSave(L"Stop Grabbing!", LV_EVENT);
}


void CGigeCamSettingDlg::OnBnClickedSoftwareOnceButton()
{

	int nErrCode = m_cGigeCam.DoSoftwareOnce();
	if (MV_OK != nErrCode)
	{
		gSystemLog.DisplayLogAndSave(L"Do Software Once Fail!", LV_ERROR);
		return;
	}
}


void CGigeCamSettingDlg::OnBnClickedButtonSaveBmp()
{
	if (!m_cGigeCam.m_bStartGrabbing) return;
	CFileDialog dlg(FALSE, L"*.bmp;*.jpg;*.tif", NULL,
		OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
		L"BMP(*.bmp)|*.bmp|JPG(*.jpg)|*.jpg|TIF(*.tif)|*.tif||", NULL);// 选项图片的约定;  
	dlg.m_ofn.lpstrTitle = _T("保存图片");// 打开文件对话框的标题名;(*.bmp; *.jpg) |*.bmp; *.jpg | 

	if (dlg.DoModal() == IDOK)// 判断是否获得图片;  
	{

		if (dlg.GetFileExt() != "bmp" && dlg.GetFileExt() != "jpg"&&dlg.GetFileExt() != "tif")
		{
			AfxMessageBox(_T("请选择正确的图片格式！"), MB_OK);
			return;
		}

		CString mPath = dlg.GetPathName();// 获取图片路径;  

		USES_CONVERSION;
		char *path = T2A(mPath);

		imwrite(path, m_cGigeCam.m_matBmpbuf);//见注4
	}
}




void CGigeCamSettingDlg::OnBnClickedButtonSetPara()
{
	UpdateData(TRUE);

	ReadEditValueToConfig();

	gSystemMgr.m_cGigeCam.m_dExpouseEdit = gSystemMgr.m_sCameraInf.m_fExposureTime;
	int nErrCode = gSystemMgr.m_cGigeCam.SetExposureTime();
	if (nErrCode != MV_OK)
	{
		gSystemLog.DisplayLogAndSave(L"相机设置曝光失败", LV_ERROR);
		return;
	}
	gSystemLog.DisplayLogAndSave(L"相机设备打开成功！", LV_EVENT);
}
